var app = getApp();
var page;
var listsize

Page({

    data: {
        winWidth: 0,
        winHeight: 0,
        // tab切换  
        currentTab: 0,
        pay: '微信支付'
    },

    onLoad: function(options) {
        var that = this;
        
        var orderState = options.status;
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    winWidth: res.windowWidth,
                    winHeight: res.windowHeight
                });
            }
        });

        var currentTab = that.data.currentTab;
        var openId = app.globalData.openId;
        if (orderState ==-1) {
            currentTab = 0
        } else if (orderState == 1) {
          currentTab = 1;
        } else if (orderState == 2) {
          currentTab = 2
        } else if (orderState == 5) {
          currentTab = 3
        }

        that.setData({
            orderState: orderState,
            currentTab: currentTab
        })

        // 刷新列表
        page = 1;
        that.getOrderList(orderState)

    },

    /** 
     * 滑动切换tab 
     */
    bindChange: function(e) {
        var orderState = '';
        var current = e.detail.current;
        var that = this;
        that.setData({
            currentTab: current
        });

        var currentTab = that.data.currentTab;
        var openId = app.globalData.openId;
        if (that.data.currentTab == 0) {
            orderState = -1;
        } else if (that.data.currentTab == 1) {
            orderState = 1;
        } else if (that.data.currentTab == 2) {
            orderState = 2
        } else if (that.data.currentTab == 3) {
            orderState = 5
        }
        // 切换tab刷新列表
        page = 1;
        that.getOrderList(orderState)

        that.setData({
            orderState: orderState,
            // currentTab: currentTab
        })

       
    },
    /** 
     * 点击tab切换 
     */
    swichNav: function(e) {
        var orderState = '';
        var that = this;
        // var currentTab = that.data.currentTab;

        if (that.data.currentTab === e.target.dataset.current) {
            return false;
        } else {
            that.setData({
                currentTab: e.target.dataset.current
            })
        }

        if (that.data.currentTab == 0) {
            orderState = -1;
        } else if (that.data.currentTab == 1) {
            orderState = 1;
        } else if (that.data.currentTab == 2) {
            orderState = 2
        } else if (that.data.currentTab == 3) {
            orderState = 5
        }

        // 切换tab刷新列表
        page = 1;
        that.getOrderList(orderState)
        that.setData({
            orderState: orderState,
        })
    },
//  订单列表
  getOrderList: function (orderState){
    var that = this
    wx.showLoading({
      title: '正在加载',
      mask: true,
    })
    that.setData({
      allorderdata: [],
    })
    var openId = app.globalData.openId;
    if(page === 1){
     var list = [];
     var sum = 120;

    }else{
     var list = that.data.allorderdata
     var sum = that.data.sum;
    }
  wx.request({
    url: app.globalData.url + 'getOrderListByOrderState?openId=' + openId + '&orderState=' + orderState,
    success: function (res) {
      var allorderdata = res.data[0];
    
      allorderdata.forEach((data, index) => {
        switch (data.orderState){
          case 0:
          data.state_str='交易失败';
          data.events = [];
          break;
          case 1:
            data.state_str = '待支付';
            data.events = [{
              name:'付款',
              event: 'onPayAgin'
            },{
                name: '关闭',
              event: 'onClose'
            }];
           
            break;
          case 2:
            data.state_str = '待发货';
            data.events = [, {
              name: '查看详情',
              event: 'todetail'
            }];
           
            break;
          case 3:
            data.state_str = '待接单';
            data.events = [];
            break;
          case 4:
            data.state_str = '派单中';
            data.events = [];
            break;
          case 5:
            data.state_str = '交易成功';
            data.events = [{
              name: '查看详情',
              event: 'todetail'
            },{
              name: '评价',
              event:'onEvaluate'
              }];
           
            break;
        }
      });
    

      page++; //分页页码
      listsize == allorderdata.length;
      if(listsize == 0){
        var more = '没有更多啦'
      }else{
        var more = '加载更多'
      }

      that.setData({
        allorderdata: list.concat(allorderdata),
        more:more
      })
    },
    fail:function(){

    },
    complete:function(){
      setTimeout(function(){
        wx.hideLoading()
        wx.stopPullDownRefresh()
      },500)
    }
  })
},
//  打开详情
  todetail:function(e){
    var o = e.curendtTarget.dataset
    wx.navigateTo({
      url: '/pagea/mine/allorder/orderdetail?id='+o.id,
    })

  },
    //付款
    onPayAgin: function(e) {
        wx.showToast({
            title: '付款中..',
        })
        var id = e.currentTarget.dataset.id;
        var openId = app.globalData.openId;
        wx.request({
            url: app.globalData.url+ 'toRequestPayAgin?openId=' + openId + '&orderId=' + id,
            success: function(res) {
                var agindata = res.data[0];
                var orderNumber = agindata.orderNumber;
                var orgId = agindata.orgId;
                var money = agindata.money;
                var types = agindata.type;

                wx.request({
                    url: app.globalData.url+ 'toPayOrder?openId=' + openId +
                        '&orderNumber=' + orderNumber + '&orgId=' + orgId + '&money=' +
                        money + '&type=' + types,
                    success: function(res) {
                        var wxpaydata = res.data[0];
                        var nonceStr = wxpaydata.nonceStr;
                        var packages = wxpaydata.package;
                        var sign = wxpaydata.sign;
                        var timeStamp = wxpaydata.timeStamp
                        wx.requestPayment({
                            'timeStamp': timeStamp,
                            'nonceStr': nonceStr,
                            'package': packages,
                            'signType': 'MD5',
                            'paySign': sign,
                            'success': function(res) {

                                wx.hideLoading();

                                wx.showToast({
                                    title: '支付成功',
                                })

                                wx.request({
                                    url: app.globalData.url+ 'updateOrderStateByOrderId?openId=' + openId +
                                        '&orderId=' + id,
                                    success: function(res) {
                                        console.log('updateOrderStateByOrderId', res.data)
                                    }
                                })

                                setTimeout(function() {
                                    wx.redirectTo({
                                        url: '/pages/mine/allorder/allorder',
                                    })
                                }, 1000)

                            },
                            'fail': function(res) {
                                wx.hideLoading();
                                wx.showToast({
                                    title: '支付失败',
                                })
                            }
                        })
                    }
                })
            }
        })
    },

    //关闭
    onClose: function(e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        var openId = app.globalData.openId;
        var orderState = that.data.orderState;
        wx.showModal({
            title: '提示',
            content: '是否关闭此订单',
            success: function(res) {
                if (res.confirm) {
                    wx.request({
                        url: app.globalData.url+ 'deleteOrder?openId=' + openId + '&orderId=' + id,
                        success: function(res) {
                            if (res.data == 'ajaxSuccess') {
                                wx.showToast({
                                    title: '已关闭',
                                })
                                wx.request({
                                    url: app.globalData.url+ 'getOrderListByOrderState?openId=' + openId + '&orderState=' + orderState,
                                    success: function(res) {
                                        var allorderdata = res.data[0];
                                        that.setData({
                                            allorderdata: allorderdata,
                                        })
                                    }
                                })
                            }
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },


    //物流
    onWL: function(e) {

    },

    //评价
    onEvaluate: function(e) {
        var pl = e.currentTarget.dataset.pl;
        var types = e.currentTarget.dataset.type;
        var id = e.currentTarget.dataset.id;
        var sid = e.currentTarget.dataset.sid;
        var url = e.currentTarget.dataset.url;
        var name = e.currentTarget.dataset.name;
        var shop = e.currentTarget.dataset.shop;
        var surl = e.currentTarget.dataset.surl;
        if (pl == 0 && types !== 1) {
            wx.redirectTo({
                url: '/pages/mine/allorder/evaluate/evaluate?types=' + types + '&id=' + id + '&sid=' + sid + '&url=' + url + '&name=' + name + '&shop=' + shop + '&surl=' + surl,
            })
        } else if (pl == 1 || types == 1) {
            wx.showToast({
                title: '不可评价',
            })
        }

    },
    
    onPullDownRefresh:function(){
      // 下拉刷新列表
      page = 1;
      that.getOrderList(orderState)
    },
    onReachBottom:function(){
      // 上拉加载
      if(listsize > 0){
        that.getOrderList(orderState)
      }
    }
})